package com.wqd.kafka.sample;

import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.*;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

/**
 * 流式处理
 */
public class KafkaStreamQuickStart {

    public static void main(String[] args) {

        //kafka的配置信心
        Properties prop = new Properties();
        prop.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.130:9092");
        prop.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        prop.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        prop.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-quickstart");

        //stream 构建器
        StreamsBuilder streamsBuilder = new StreamsBuilder();

        //流式计算
        streamProcessor(streamsBuilder);


        //创建kafkaStream对象
        KafkaStreams kafkaStreams = new KafkaStreams(streamsBuilder.build(), prop);
        //开启流式计算
        kafkaStreams.start();
    }

    /**
     * 流式计算处理的业务
     * 求单词个数（word count）
     * 生成者发送消息，hello  itcast
     *
     * @param streamsBuilder
     */
    private static void streamProcessor(StreamsBuilder streamsBuilder) {
        //1.接收生产者发送的消息,指定生产者的topic
        KStream<String, String> stream = streamsBuilder.stream("kafkastream-topic1");
        //2.处理消息
        stream.flatMapValues(new ValueMapper<String, Iterable<String>>() {
            /**
             * 接收的消息：
             *      key=first0  value=hello kafka
             *      key=first1  value=hello itcast
             *
             * @param value
             * @return ["hello","kafka","hello","itcast",....]
             */
            @Override
            public Iterable<String> apply(String value) {
                return Arrays.asList(value.split(" "));
            }
        })
                /**
                 * 表示分组聚合，key=first0 first1 ,....
                 * 此时的value就是 ["hello","kafka","hello","itcast",....]
                 *          分组之后就会出现三组，以hello一组，kafka一组，itcast一组
                 */
                .groupBy((key, value) -> value)//根据什么分组
                .windowedBy(TimeWindows.of(Duration.ofSeconds(10)))//每10s拉取一次消息内容
                .count()//求和，返回值是ktable类型
                .toStream()//把ktable类型转化成kstream类型
                /**
                 * 发送的消息内容
                 * key=hello  ,kafka, itcast
                 * value=2     2            2
                 */
                .map((KeyValueMapper<Windowed<String>, Long, KeyValue<String, String>>) (key, value) -> {
                    return new KeyValue<>(key.key().toString(), value.toString());
                })
                //第三步，发送消息，消息内容,消费者监听的topic
                .to("kafkastream-toipc2");//发送消息;




    }
}